package com.taotao.sys.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.taotao.common.RespData;
import com.taotao.common.auth.LoginService;
import com.taotao.common.auth.UserInfo;
import com.taotao.slink.service.CaptchaService;
import com.taotao.sys.model.vo.login.LoginInfo;
import com.taotao.sys.model.vo.login.LoginParam;
import com.taotao.sys.model.vo.login.Token;
import com.taotao.common.auth.satoken.SatokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;
import javax.validation.Valid;
import java.util.Set;


@Api(tags = "登录接口")
@Validated
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private CaptchaService captchaService;


    @ApiOperation(value = "登录", notes = "")
    @PostMapping(value = "/login")
    public RespData<Token> login(@Valid @RequestBody LoginParam param) throws LoginException {
        captchaService.checkVerifyCode(param);

        String token = loginService.login(param);

        return RespData.successResp(new Token(token));
    }


    @ApiOperation(value = "是否登录", notes = "")
    @GetMapping(value = "/isLogin")
    public RespData<UserInfo> isLogin() {
        String token = SatokenUtils.getClinetToken();
        if (StringUtils.isBlank(token)) {
            return RespData.failedResp("未登录");
        }

        UserInfo userInfo = loginService.getLoginUserInfo(token);

        if (null == userInfo) {
            return RespData.failedResp("未登录");
        }

        return RespData.successResp(userInfo);

    }


    @ApiOperation(value = "退出", notes = "")
    @PostMapping(value = "/logout")
    public RespData<Object> logout() {

        SatokenUtils.logout(SatokenUtils.getClinetToken());

        return RespData.successResp();

    }


    @ApiOperation(value = "获取登录信息(包含菜单权限)", notes = "")
    @GetMapping(value = "/info")
    public RespData<LoginInfo> info() {

        String userId = (String) StpUtil.getLoginId();
        String token = StpUtil.getTokenValue();
        UserInfo userInfo = loginService.getLoginUserInfo(token);
        Set<String> funcList = loginService.getUserFuncList(Long.parseLong(userId));

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setName(userInfo.getNickName());
        loginInfo.setRoles(funcList);
        loginInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");


        return RespData.successResp(loginInfo);

    }

}
